#ifndef QNOTETHUMBNAILLISTITEMWIDGET_H
#define QNOTETHUMBNAILLISTITEMWIDGET_H

#include <MWidget>
#include <MContentItem>
#include "QvernoteAPI.h"
#include <QGraphicsPixmapItem>
#include <MAbstractCellCreator>
#include <MApplication>
#include "QEnmlReader.h"

class QNoteThumbnailListItemWidget : public MAbstractCellCreator<MContentItem>
{
    //Q_OBJECT

public:
    QNoteThumbnailListItemWidget(bool isDeleteMode = false) : m_bDeleteMode(isDeleteMode) {
        setCellViewType("NoteThumbnailListItem");
    }

    ~QNoteThumbnailListItemWidget() {}

    virtual void updateCell (const QModelIndex &index, MWidget *cell) const {
        MContentItem* m = qobject_cast<MContentItem*>(cell);
        QVariant data = index.data(Qt::DisplayRole);
        int noteptr = data.value<int>();
        Note* note = (Note*)(int*)noteptr;
        QByteArray  thumbnail;
        QPixmap*    thumbnailImage;

        /* draw note tags */
        vector<string>::iterator it;
        QString tagList = 0;
        for(it = note->tagNames.begin(); it < note->tagNames.end(); it++)
        {
                tagList += QString((*it).c_str()) + " ";
        }

        if(tagList.size() == 0)
                m->setSubtitle(QApplication::trUtf8("No tags"));
        else
                m->setSubtitle(tagList);

        QString noteTitle = QString::fromUtf8(note->title.c_str());

        //TODO m->lNoteTitle->setStyleSheet("color: black");
        m->setTitle(noteTitle);

        if(QvernoteStorage::Instance()->getNoteThumbnail(note->guid, thumbnail))
        {
                thumbnailImage = new QPixmap();
                thumbnailImage->loadFromData(thumbnail);
                m->setPixmap(thumbnailImage->copy());
                delete thumbnailImage;
        }
        else
        {
            QvernoteAPI::Instance()->getNoteContent(*note);
            QEnmlReader* enmlReader = new QEnmlReader(*note);
            enmlReader->setLoadExternalReferences(false);
            enmlReader->parse();
            enmlReader->createThumbnail();
            //TODO: Possbile problem with async note rendering
            m->setPixmap(enmlReader->getThumbnailCreator()->getThumbnail());
            delete enmlReader;
        }
    }

private:
    bool m_bDeleteMode;
};

#endif // QNOTETHUMBNAILLISTITEMWIDGET_H
